/*
 * Copyright (C) 2013 Zhang Rui <bbcallen@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hoopmedia.hoopmediaplayerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import tv.danmaku.ijk.media.example.widget.media.IjkVideoView;
import tv.danmaku.ijk.media.player.IMediaPlayer;


public class AcVideoPlayerActivity extends Activity {
	private IjkVideoView mVideoView;
	private String mVideoPath;
	private String TAG="AcSdk_main";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mVideoPath = getIntent().getExtras().getString("RtmpAddress");

		mVideoView = new IjkVideoView(this);
		mVideoView.setOnErrorListener(mErrorListener);
		mVideoView.setOnCompletionListener(mCompletionListener);
		mVideoView.setOnPreparedListener(mPreparedListener);

		setContentView(mVideoView);

		StartPlayer();
	}

	private IMediaPlayer.OnPreparedListener mPreparedListener = new IMediaPlayer.OnPreparedListener() {
		@Override
		public void onPrepared(IMediaPlayer mp) {
		}
	};


	private void onStopAll(boolean isDestroy)
	{
		Log.w(TAG, "stop-all, destroy:"+isDestroy);
		mVideoView.stopPlayback();
		mVideoView.release(true);
		if (isDestroy)
		{
			mVideoView.onActDestroy();
		}
	}

	@Override
	protected void onStop() {
		onStopAll(false);
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		onStopAll(true);
		super.onDestroy();
	}

	void StartPlayer()
	{
		String path = mVideoPath;
		String pubKey =  "fef19718136b161290d8d116b9912810";
		String prjId =  "f88624ad0cf2380b22100336";
		String deviceId = "123456789";
		String roomId = "12305";
		String roomName = "jingcaijinqiu";
		String channelId = "360market";
		String sectionlId = "2011";
		boolean isRegister = false;
		String reserved = "reserved";

		mVideoView.setVideoPara(path, pubKey, prjId, deviceId, roomId, roomName, channelId, sectionlId, isRegister, reserved);
		//mVideoView.setVideoPath(mVideoPath, "fef19718136b161290d8d116b9912810");
		mVideoView.start();
	}

	public class ConnectRunable implements Runnable{
		public void run() {
			StartPlayer();
			Log.d("123", "ConnectRunable ~~~~~~~~~~~~~~~~");
		}
	}

	public Handler handler = new Handler();
	private IMediaPlayer.OnErrorListener mErrorListener = new IMediaPlayer.OnErrorListener() {

		@Override
		public boolean onError(IMediaPlayer arg0, int arg1, int arg2) {
			onStopAll(true);

			handler.postDelayed(new ConnectRunable(), 3000);

			return true;
		}
	};

	private IMediaPlayer.OnCompletionListener mCompletionListener = new IMediaPlayer.OnCompletionListener() {

		@Override
		public void onCompletion(IMediaPlayer arg0) {
			onStopAll(true);
		}
	};
}

